package cn.iocoder.yudao.module.cms.qlink.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cms.qlink.dal.dataobject.QlinkDO;
import cn.iocoder.yudao.module.cms.qlink.service.QlinkService;
import cn.iocoder.yudao.module.cms.qlink.vo.AdminQlinkPageReqVO;
import cn.iocoder.yudao.module.cms.qlink.vo.AdminQlinkRespVO;
import cn.iocoder.yudao.module.cms.qlink.vo.AdminQlinkSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 快捷链接")
@RestController
@RequestMapping("/cms/qlink")
@Validated
public class QlinkController {

    @Resource
    private QlinkService qlinkService;

    @PostMapping("/create")
    @Operation(summary = "创建快捷链接")
    @PreAuthorize("@ss.hasPermission('cms:qlink:create')")
    public CommonResult<Long> createQlink(@Valid @RequestBody AdminQlinkSaveReqVO createReqVO) {
        return success(qlinkService.createQlink(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新快捷链接")
    @PreAuthorize("@ss.hasPermission('cms:qlink:update')")
    public CommonResult<Boolean> updateQlink(@Valid @RequestBody AdminQlinkSaveReqVO updateReqVO) {
        qlinkService.updateQlink(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除快捷链接")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:qlink:delete')")
    public CommonResult<Boolean> deleteQlink(@RequestParam("id") Long id) {
        qlinkService.deleteQlink(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得快捷链接")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:qlink:query')")
    public CommonResult<AdminQlinkRespVO> getQlink(@RequestParam("id") Long id) {
        QlinkDO ad = qlinkService.getQlinkById(id);
        return success(BeanUtils.toBean(ad, AdminQlinkRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得快捷链接分页")
    @PreAuthorize("@ss.hasPermission('cms:qlink:query')")
    public CommonResult<PageResult<AdminQlinkRespVO>> getAdPage(@Valid AdminQlinkPageReqVO pageReqVO) {
        PageResult<QlinkDO> pageResult = qlinkService.getQlinkPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AdminQlinkRespVO.class));
    }

}
